<?php


namespace app\shopConsole\controller;


use app\BaseController;
use app\lib\exception\ParamException;
use app\Request;
use app\shopConsole\service\role\ShopRoleQueryService;
use app\shopConsole\validate\RoleValidate;
use think\App;
use think\exception\ValidateException;

function shopRole2dict($list)
{
    $dateTime = app('DateTime');

    return [
        'id' => $list['id'],
        'name' => $list['name'],
        'shop_id' => $list['shop_id'],
        'finished_product_discount' => $list['finished_product_discount'],
        'semi_finished_product_discount' => $list['semi_finished_product_discount'],
        'applicable' => $list['applicable'],
        'serviceable' => $list['serviceable'],
        'commission_ratio' => $list['commission_ratio'],
        'create_time' => $list['create_time'],
        'update_time' => $list['update_time'],
        'format_create_time' => $dateTime->timestamp2date($list['create_time']),
        'format_update_time' => $dateTime->timestamp2date($list['update_time']),
    ];
};

//标签
class RoleController extends BaseController
{
    public function get(Request $request, ShopRoleQueryService $shopRoleQueryService)
    {
        $page = $request->get('page', false);
        $perPage = $request->get('perPage', config('app.allow_limit'));
        $all = $request->get('all', 1);

        if (!$all)
        {
            try
            {
                validate(RoleValidate::class)
                    ->scene('get')
                    ->check([
                        'page'         => $page ,
                        'perPage'      => $perPage ,
                    ]);
            } catch (ValidateException $e) {
                throw new ParamException($e->getMessage());
            }
            $roles = $shopRoleQueryService->getPageListByShopId($request->id, $page ,$perPage);
        }else{
            $roles = $shopRoleQueryService->getListByShopId($request->id);
        }

        return $this->returnList(
            array_map(fn($list) => shopRole2dict($list), $roles['list']->toArray()),
            $roles['pagination']['page'], $roles['pagination']['perPage'], $roles['pagination']['total']
        );
    }
}